package io.demo.hallelujahflowable6.config;

import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.flowable.cmmn.engine.CmmnEngineConfiguration;
import org.flowable.cmmn.spring.CmmnEngineFactoryBean;
import org.flowable.cmmn.spring.SpringCmmnEngineConfiguration;
import org.flowable.dmn.engine.DmnEngine;
import org.flowable.dmn.engine.DmnEngineConfiguration;
import org.flowable.dmn.spring.DmnEngineFactoryBean;
import org.flowable.dmn.spring.SpringDmnEngineConfiguration;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.common.impl.cfg.IdGenerator;
import org.flowable.engine.common.impl.persistence.StrongUuidGenerator;
import org.flowable.form.engine.FormEngine;
import org.flowable.form.engine.FormEngineConfiguration;
import org.flowable.form.spring.FormEngineFactoryBean;
import org.flowable.form.spring.SpringFormEngineConfiguration;
import org.flowable.spring.boot.ProcessEngineConfigurationConfigurer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * @author lijinting01
 */
@Slf4j
@Configuration
public class FlowableConfig {
	/**
	 * 在启动日志中打印引擎信息
	 *
	 * @param processEngine 流程引擎
	 * @param formEngine    表单引擎
	 * @param dmnEngine     决策引擎
	 * @return
	 */
	@Bean
	public InitializingBean engineInfoLoggingBean(
			ProcessEngine processEngine,
			FormEngine formEngine,
			DmnEngine dmnEngine) {
		return () -> {
			log.info("ProcessEngine name: {} FormEngine name: {}, DmnEngine name: {}",
					processEngine.getName(),
					formEngine.getName(),
					dmnEngine.getName());
		};
	}

	/**
	 * 自定义流程引擎
	 *
	 * @param idGenerator 标识生成器
	 * @return 自定义流程引擎器
	 */
	@Bean
	public ProcessEngineConfigurationConfigurer processEngineConfigurationConfigurer(IdGenerator idGenerator) {
		return cfg -> {
			cfg.setIdGenerator(idGenerator);
		};
	}

	/**
	 * @param formEngineConfiguration 表单引擎配置
	 * @param applicationContext      上下文
	 * @return
	 */
	@Bean
	public FormEngineFactoryBean formEngineFactoryBean(
			FormEngineConfiguration formEngineConfiguration,
			ApplicationContext applicationContext) {
		FormEngineFactoryBean formEngineFactoryBean = new FormEngineFactoryBean();
		formEngineFactoryBean.setFormEngineConfiguration(formEngineConfiguration);
		formEngineFactoryBean.setApplicationContext(applicationContext);
		return formEngineFactoryBean;
	}

	/**
	 * @param dmnEngineConfiguration
	 * @param applicationContext
	 * @return
	 */
	@Bean
	public DmnEngineFactoryBean dmnEngineFactoryBean(
			DmnEngineConfiguration dmnEngineConfiguration,
			ApplicationContext applicationContext) {
		DmnEngineFactoryBean dmnEngineFactoryBean = new DmnEngineFactoryBean();
		dmnEngineFactoryBean.setDmnEngineConfiguration(dmnEngineConfiguration);
		dmnEngineFactoryBean.setApplicationContext(applicationContext);
		return dmnEngineFactoryBean;
	}

	/**
	 * @param cmmnEngineConfiguration
	 * @param applicationContext
	 * @return
	 */
	@Bean
	public CmmnEngineFactoryBean cmmnEngineFactoryBean(
			CmmnEngineConfiguration cmmnEngineConfiguration,
			ApplicationContext applicationContext) {
		CmmnEngineFactoryBean cmmnEngineFactoryBean = new CmmnEngineFactoryBean();
		cmmnEngineFactoryBean.setCmmnEngineConfiguration(cmmnEngineConfiguration);
		cmmnEngineFactoryBean.setApplicationContext(applicationContext);
		return cmmnEngineFactoryBean;
	}


	/**
	 * @param txManager   事务管理器
	 * @param dataSource  数据源
	 * @param idGenerator ID生成器
	 * @return
	 */
	@Bean
	public SpringFormEngineConfiguration formEngineConfiguration(
			PlatformTransactionManager txManager,
			HikariDataSource dataSource,
			IdGenerator idGenerator) {
		SpringFormEngineConfiguration configuration = new SpringFormEngineConfiguration();
		configuration.setTransactionManager(txManager);
		configuration.setDataSource(dataSource);
		configuration.setIdGenerator(idGenerator);
		return configuration;
	}

	/**
	 * @param txManager   事务管理器
	 * @param dataSource  数据源
	 * @param idGenerator ID生成器
	 * @return
	 */
	@Bean
	public SpringDmnEngineConfiguration dmnEngineConfiguration(
			PlatformTransactionManager txManager,
			HikariDataSource dataSource,
			IdGenerator idGenerator) {
		SpringDmnEngineConfiguration configuration = new SpringDmnEngineConfiguration();
		configuration.setDataSource(dataSource);
		configuration.setIdGenerator(idGenerator);
		configuration.setTransactionManager(txManager);
		return configuration;
	}


	/**
	 * @param txManager   事务管理器
	 * @param dataSource  数据源
	 * @param idGenerator ID生成器
	 * @return
	 */
	@Bean
	public SpringCmmnEngineConfiguration cmmnEngineConfiguration(
			PlatformTransactionManager txManager,
			HikariDataSource dataSource,
			IdGenerator idGenerator) {
		SpringCmmnEngineConfiguration configuration = new SpringCmmnEngineConfiguration();
		configuration.setTransactionManager(txManager);
		configuration.setDataSource(dataSource);
		configuration.setIdGenerator(idGenerator);
		return configuration;
	}

	/**
	 * 流程引擎ID提供方
	 *
	 * @return
	 */
	@Bean
	public StrongUuidGenerator strongUuidGenerator() {
		return new StrongUuidGenerator();
	}
}
